package com.example.framework.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.example.framework.R;
import com.example.framework.manager.DialogManager;
import com.example.framework.utils.AnimUtils;

/**
 * @author:0.2℃
 * @date： 2022-04-03 18:34
 * @Description ：
 */
public class LoadingView {
    private DialogView mLoadingView;
    private ImageView iv_loading;
    private TextView tv_loading;
    private ObjectAnimator mAnim;

    public LoadingView(Context mContext) {
        mLoadingView = DialogManager.getDialogInstance().initView(mContext, R.layout.dialog_loading);
        iv_loading = mLoadingView.findViewById(R.id.iv_loading);
        tv_loading = mLoadingView.findViewById(R.id.tv_loading);
        mAnim = AnimUtils.rotation(iv_loading);
    }

    /**
     * 设置加载的提示文本
     * @param text
     */
    public void setLoadingText(String text) {
        if (!TextUtils.isEmpty(text)) {
            tv_loading.setText(text);
        }
    }

    public void show() {
        mAnim.start();
        DialogManager.getDialogInstance().showDialog(mLoadingView);
    }

    public void show(String text) {
        mAnim.start();
        setLoadingText(text);
        DialogManager.getDialogInstance().showDialog(mLoadingView);
    }

    public void hide() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mAnim.pause();
        }
        DialogManager.getDialogInstance().hideDialog(mLoadingView);
    }
}
